//弹出气泡标题设置
function Popup_template_set_sub(Popup_Content) {
    var context = "";
    var Fields = Popup_Content.split(";");
    for (var i = 0; i < Fields.length; i++) {
        var attr = Fields[i].split("@");
        context = context + "<b>" + attr[0] + ":  </b>{" + attr[1] + "}<br/>";
    }
    return context;
}
//弹出气泡内容设置
function Popup_template_content_set(Popup_Content) {
    var content = Popup_Content;
    var catyname = content.graphic.attributes.城市;
    var query_cityname = toPinyin(catyname);
    var zhandianname = content.graphic.attributes.站点;
    var context = "";
    context = context + "<b>城市:  </b>" + catyname + "<br/>";
    context = context + "<b>站点:  </b>" + zhandianname + "<br/>";
    context = context + "<span id='contentaqi'></span>";
    var contenaqi = "";
    $.ajax({
        url: 'http://api.help.bj.cn/apis/aqi3',
        async: true,
        data: {
            "id": query_cityname.toString()
        },
        type: 'GET',
        dataType: 'json',
        success: function (aqidata) {
            var zahndian_arry = aqidata.data;
            for (var i = 0; i < zahndian_arry.length; i++) {
                if (zahndian_arry[i].position_name == zhandianname) {
                    contenaqi = contenaqi + "<b>AQI:  </b>" + zahndian_arry[i].aqi + "<br/>";
                    contenaqi = contenaqi + "<b>污染级别:  </b>" + zahndian_arry[i].level + "<br/>";
                    contenaqi = contenaqi + "<b>pm25:  </b>" + zahndian_arry[i].pm25 + "<br/>";
                    contenaqi = contenaqi + "<b>pm10:  </b>" + zahndian_arry[i].pm10 + "<br/>";
                    contenaqi = contenaqi + "<b>co:  </b>" + zahndian_arry[i].co + "<br/>";
                    contenaqi = contenaqi + "<b>no2:  </b>" + zahndian_arry[i].no2 + "<br/>";
                    contenaqi = contenaqi + "<b>so2:  </b>" + zahndian_arry[i].so2 + "<br/>";
                }
            };
            contenaqi = contenaqi + "<b>更新时间:  </b>" + aqidata.update + "<br/>";
            $("#contentaqi").html(contenaqi);
        },
        error: function (e) {
        },
    });
    return context;
}
//Popup按钮设置
function Popup_Button_set(Popup_Content) {
    var Popup_Button_layer = new Array()
    var Fields = Popup_Content.split(";");
    for (var i = 0; i < Fields.length; i++) {
        for (var ii = 0; ii < Popup_Buttons.length; ii++) {
            if (Popup_Buttons[ii].title == Fields[i]) {
                Popup_Button_layer[i] = Popup_Buttons[ii];
            }
        }
    }
    return Popup_Button_layer;
}

//Pane窗口显示
function Pane_visible(targetid, nav_close) {
    var target = dijit.byId(targetid);
    if (target.domNode.style.visibility == 'hidden' || target.domNode.style.display == 'none') {
        target.show();
    }
    target.bringToTop();
}

//Pane窗口关闭
function Pane_invisible(targetid) {
    var target = dijit.byId(targetid);
    if (target.domNode.style.visibility != 'hidden') {
        target.hide();
    }
}
//Pane窗口切换
function Pane_Convent(show, hide) {
    var target1 = dijit.byId(hide);
    var target2 = dijit.byId(show);
    if (target1.domNode.style.visibility != 'hidden') {
        target1.hide();
    }
    if (target2.domNode.style.visibility == 'hidden' || target2.domNode.style.display == 'none') {
        target2.show();
        target2.bringToTop();
    }
}

//图层列表,图层可视变化
function checkbox_LayerList(checkbox_layer) {
    checkbox_icon_change(checkbox_layer);
    var layer_i = checkbox_layer.value;
    var Map_Layer = Map_Layers[layer_i];
    if (Map_Layer.sublayers != null) {
        if (checkbox_layer.checked) {
            LayerList_sublayers(layer_i);
            // map_sublayer_show(layer_i, Map_Layer.sublayers.show_sublayer);
        } else {
            for (var j = 0; j < Map_Layer.sublayers.layers.length; j++) {
                var sublayer = Map_Layer.sublayers.layers[j];
                var layer_name = Map_Layer.Name + sublayer.name;
                var layer = myMap.findLayerById(layer_name);
                layer.visible = false;
            }
        }
    } else {
        var layer = myMap.findLayerById(Map_Layer.Name);
        layer.visible = checkbox_layer.checked;
    }
}
//checkbox图标更改
function checkbox_icon_change(checkbox_layer) {
    var littlepic = checkbox_layer.parentNode.children[0];
    if (checkbox_layer.checked) {
        littlepic.innerHTML = '&#xe637;';

        littlepic.className = 'icon boxlttle check cp active';
    } else {
        littlepic.innerHTML = '';
        littlepic.className = 'icon boxlttle check cp';
    }
}

//清除按钮
function Clear_all() {
    document.getElementById('mapDiv').style.cursor = 'default';
    graphicsLayer_buffer.removeAll();
    //清除克里金插值分析结果
    if (myMap.findLayerById("克里金")) {
        myMap.remove(myMap.findLayerById("克里金"));
    }

}

//画图
function map_draw_start(type) {
    //把类型存入全局变量，当类型为1时为克里金插值分析，0位画图
    map_draw_type = type;
    if (map_draw_type == 1) {
        $('#span_draw_type').html("范围：");
        $('#button_draw_chazhi').show();
        $('#button_draw_buffer').hide();
    } else {
        $('#span_draw_type').html("画图：");
        $('#button_draw_buffer').show();
        $('#button_draw_chazhi').hide();
    }
    Pane_visible('Pane_Draw');
    Pane_invisible('Pane_Buffer');
}
//画图图标点击开始画图
function map_draw_geometry_start(geometry_type) {
    document.getElementById('mapDiv').style.cursor = 'crosshair';
    map_draw_geometry = geometry_type;
    Pane_invisible('Pane_Buffer');
}
//画图关闭按钮
function Pane_Draw_Close() {
    set_button_disable("button_draw_buffer", true);
    Pane_invisible('Pane_Draw');
    map_draw_end();
}
function map_draw_end() {
    Pane_invisible('Pane_Buffer');
    map_draw_geometry = "";
    Point_Map_Click = [];
    document.getElementById('mapDiv').style.cursor = 'default';
}
function map_draw_pane_close() {
    if (map_draw_type > 0) {
        Pane_Draw_Close();
        map_draw_type = 0;
    }
}
//设置按钮不可用样式
function set_button_disable(button_name, disable) {
    if (disable) {
        var button = "#" + button_name;
        $(button).attr('disabled', true);
        $(button).removeClass("gray");
        $(button).addClass("disable");
    } else {
        var button = "#" + button_name;
        $(button).attr('disabled', false);
        $(button).removeClass("disable");
        $(button).addClass("gray");
    }
}
//清除画图
function Clear_buffer() {
    set_button_disable("button_draw_buffer", true);
    document.getElementById('mapDiv').style.cursor = 'default';
    graphicsLayer_buffer.removeAll();
    map_draw_end();
    search_geometry_clear();
    if (myMap.findLayerById("克里金")) {
        myMap.remove(myMap.findLayerById("克里金"));
    }
}
//清除查询时的geometry绑定
function search_geometry_clear() {
    search_geometry = false;
    $("#button_search_zone_make").removeClass("orange");
    $("#button_search_zone_make").addClass("gray");
    $("#button_tongji_zone_make").removeClass("orange");
    $("#button_tongji_zone_make").addClass("gray");
}

//查询时的geometry设置
function search_geometry_set_new(type) {
    if (type == 1) {
        search_geometry = true;
    }
}

//AQI指数预测_获取数据
function aqi_forecast() {
    $.ajax({
        url: 'http://172.18.63.38:10001/AirDay/GetAQISenvenInfo',
        type: 'GET',
        dataType: 'json',
        success: function (aqidata) {
            var aqi_List = aqidata.rows;
            aqi_forecast_start(aqi_List);
        },
        error: function (e) {
        },
    });
}
//AQI指数预测_开始预测
function aqi_forecast_start(aqi_List) {
    //初始化平滑系数 默认为0.4
    var modulus = 0.4;
    var modulusLeft = 0.6;
    //初始化初始值 默认为前三条数据的平均值
    var lastIndex = (parseInt(aqi_List[0].AQI) + parseInt(aqi_List[1].AQI) + parseInt(aqi_List[2].AQI)) / 3;
    //初始化平滑值
    var lastSecIndex = parseInt(aqi_List[0].AQI);
    var line_data = [];
    for (var i = 0; i < aqi_List.length; i++) {
        var aqi_array = [aqi_List[i].日期.substr(6, 4)+"-"+aqi_List[i].日期.substr(0, 2)+"-"+aqi_List[i].日期.substr(3, 2), parseInt(aqi_List[i].AQI)];
        line_data.push(aqi_array);
        lastIndex = modulus * parseInt(aqi_List[i].AQI) + modulusLeft * lastIndex;
        lastSecIndex = modulus * lastIndex + modulusLeft * lastSecIndex;
    }
    var a = 2 * lastIndex - lastSecIndex;
    var b = (modulus / modulusLeft) * (lastIndex - lastSecIndex);
    var today_aqi = a + b * 1;
    var tommory_aqi = a + b * 2;
    var tommory_tommory_aqi = a + b * 3;
    var today_Date = new Date().toLocaleDateString();

    line_data.push([GetDateStr(0), today_aqi]);
    line_data.push([GetDateStr(1), tommory_aqi]);
    line_data.push([GetDateStr(2), tommory_tommory_aqi]);
    make_aqi_line_charts(line_data);
}
//获取时间字符串
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期 
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期 
    var d = dd.getDate();
    return y + "-" + m + "-" + d;
}
function make_aqi_line_charts(aqi_array) {
    var option = {
        title: {
            text: 'AQI指数预测'
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            data: aqi_array.map(function (item) {
                return item[0];
            })
        },
        yAxis: {
            splitLine: {
                show: false
            }
        },
        toolbox: {
            left: 'center',
            feature: {
                dataZoom: {
                    yAxisIndex: 'none'
                },
                restore: {},
                saveAsImage: {}
            }
        },
        dataZoom: [{
            type: 'inside'
        }],
        visualMap: {
            top: 10,
            right: 10,
            pieces: [{
                gt: 0,
                lte: 60,
                color: '#096'
            }, {
                gt: 60,
                lte: 90,
                color: '#ffde33'
            }, {
                gt: 90,
                lte: 120,
                color: '#ff9933'
            }, {
                gt: 120,
                lte: 150,
                color: '#cc0033'
            }, {
                gt: 150,
                lte: 300,
                color: '#660099'
            }, {
                gt: 300,
                color: '#7e0023'
            }],
            outOfRange: {
                color: '#999'
            }
        },
        series: {
            name: 'AQI',
            type: 'line',
            data: aqi_array.map(function (item) {
                return item[1];
            }),
            markLine: {
                silent: true,
                data: [{
                    yAxis: 60
                }, {
                    yAxis: 90
                }, {
                    yAxis: 120
                }, {
                    yAxis: 150
                }, {
                    yAxis: 300
                }]
            }
        }
    }
    echarts.init(document.getElementById('Div_aqi_forecast')).clear();
    echarts.init(document.getElementById('Div_aqi_forecast')).setOption(option);
}